package com.sky.controller.admin;

import com.aliyun.oss.model.MultipartUpload;
import com.sky.config.AliOssConfig;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author 贾晓晨
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);
    // 阿里云上传
    @PostMapping("/upload")
    @ApiOperation("阿里云oss上传接口")
    public Result<String> aliUpload(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("阿里oss上传{}",file.getOriginalFilename());
        // 获取文件名称
        String originalFilename = file.getOriginalFilename();
        // 获取文件名称的后缀
        // 保证文件不会被覆盖
        String suffix = UUID.randomUUID().toString()+
                originalFilename.substring(originalFilename.lastIndexOf("."));
        // 上传
        String upload = aliOssUtil.upload(file.getBytes(), suffix);
        return Result.success(upload);
    }

}
